package com.sjd.mall.service.shoppingcart.impl;

import com.sjd.common.core.domain.AjaxResult;
import com.sjd.mall.domain.SjdGoodsDO;
import com.sjd.mall.domain.SjdGoodsTypeDO;
import com.sjd.mall.domain.SjdShoppingCartDO;
import com.sjd.mall.domain.dto.shoppingcart.WxShoppingCartAddRequestDTO;
import com.sjd.mall.domain.dto.shoppingcart.WxShoppingCartDeleteRequestDTO;
import com.sjd.mall.domain.dto.shoppingcart.WxShoppingCartQryResponseDTO;
import com.sjd.mall.domain.dto.shoppingcart.WxShoppingCartUpdateRequestDTO;
import com.sjd.mall.enums.GoodsClass;
import com.sjd.mall.mapper.SjdGoodsMapper;
import com.sjd.mall.mapper.SjdGoodsTypeMapper;
import com.sjd.mall.mapper.SjdShoppingCartMapper;
import com.sjd.mall.service.goods.GoodsService;
import com.sjd.mall.service.shoppingcart.WxShoppingCartService;
import com.sjd.wx.utils.ThirdSessionHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * WxShoppingCartServiceImpl
 *
 * @author seer
 * @version 1.0
 * @date 2022/1/13 10:28
 */
@Service
@Slf4j
public class WxShoppingCartServiceImpl implements WxShoppingCartService {

    @Autowired
    private SjdShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SjdGoodsMapper goodsMapper;

    @Autowired
    private SjdGoodsTypeMapper goodsTypeMapper;

    @Autowired
    private GoodsService goodsService;

    /**
     * 购物车添加
     * 只有农作物可以加购物车，
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public AjaxResult add(WxShoppingCartAddRequestDTO requestDTO) {
        SjdGoodsDO goodsDO = goodsMapper.selectByPrimaryKey(requestDTO.getGoodsId());
        Assert.notNull(goodsDO, "商品不存在 " + requestDTO.getGoodsId());
        SjdGoodsTypeDO typeDO = goodsTypeMapper.selectByPrimaryKey(goodsDO.getGoodsTypeId());
        Assert.notNull(goodsDO, "分类不存在 " + goodsDO.getGoodsTypeId());
        if (GoodsClass.GOODS != typeDO.getTypeClass()) {
            return AjaxResult.error("此商品不可加入购物车");
        }
        SjdShoppingCartDO add = new SjdShoppingCartDO();
        add.setGoodsClass(GoodsClass.GOODS);
        add.setMemberId(ThirdSessionHolder.getSjdMemberId());
        add.setGoodsId(requestDTO.getGoodsId());
        add.setGoodsQuantity(requestDTO.getGoodsQuantity());
        shoppingCartMapper.insert(add);
        return AjaxResult.success();
    }

    /**
     * 购物车查询
     *
     * @return object
     */
    @Override
    public List<WxShoppingCartQryResponseDTO> qry() {
        List<SjdShoppingCartDO> resultList = shoppingCartMapper.list(ThirdSessionHolder.getSjdMemberId());
        List<WxShoppingCartQryResponseDTO> responseList = new ArrayList<>();
        for (SjdShoppingCartDO cartDO : resultList) {
            SjdGoodsDO goodsDO = goodsMapper.selectByPrimaryKey(cartDO.getGoodsId());
            BigDecimal goodsAmount = cartDO.getGoodsQuantity().multiply(goodsDO.getGoodsPrice());
            BigDecimal goodsDiscount = goodsService.getMembershipPrice(ThirdSessionHolder.getSjdMemberId(), goodsAmount);
            BigDecimal goodsPayable = goodsAmount.subtract(goodsDiscount);

            WxShoppingCartQryResponseDTO response = new WxShoppingCartQryResponseDTO();
            response.setId(cartDO.getId());
            response.setGoodsId(cartDO.getGoodsId());
            response.setGoodsQuantity(cartDO.getGoodsQuantity());
            response.setGoodsName(goodsDO.getGoodsName());
            response.setGoodsPicture(goodsDO.getBigPicture());
            response.setGoodsPrice(goodsDO.getGoodsPrice());
            response.setGoodsAmount(goodsAmount);
            response.setDiscountAmount(goodsDiscount);
            response.setPayableAmount(goodsPayable);
            responseList.add(response);
        }
        return responseList;
    }

    /**
     * 购物车修改
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public AjaxResult update(WxShoppingCartUpdateRequestDTO requestDTO) {
        SjdShoppingCartDO result = shoppingCartMapper.getById(requestDTO.getId());
        Assert.notNull(result, "信息不存在 " + requestDTO.getId());
        shoppingCartMapper.updateQuantity(requestDTO.getGoodsQuantity(), requestDTO.getId());
        return AjaxResult.success();
    }

    /**
     * 购物车删除
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @Override
    public AjaxResult delete(WxShoppingCartDeleteRequestDTO requestDTO) {
        shoppingCartMapper.deletes(requestDTO.getIds());
        return AjaxResult.success();
    }
}
